package com.ruoyi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.UserDeviceData;
import com.ruoyi.service.UserDeviceDataService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author lifei
 */
@RestController
@RequestMapping("/kangyue/userdevicedata")
public class UserDeviceDataController extends BaseController {

    private final UserDeviceDataService userDeviceDataService;

    public UserDeviceDataController(UserDeviceDataService userDeviceDataService) {
        this.userDeviceDataService = userDeviceDataService;
    }

    // 新增：用于管理 SSE 连接的 Map
    private static final Map<Long, SseEmitter> sseEmitters = new ConcurrentHashMap<>();

    /**
     * 建立 SSE 连接以实时获取最新数据
     *
     * @param userId 用户ID
     */
    @GetMapping(path = "/streamLatestData", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter streamLatestData(@RequestParam Long userId) {
        System.out.println("SSE 连接建立，用户ID：" + userId);

        SseEmitter emitter = new SseEmitter(60_000L); // 超时时间 60 秒
        sseEmitters.put(userId, emitter);

        // 设置连接关闭时的清理逻辑
        emitter.onCompletion(() -> sseEmitters.remove(userId));
        emitter.onTimeout(() -> sseEmitters.remove(userId));
        emitter.onError((e) -> sseEmitters.remove(userId));

        return emitter;
    }

    // 在 UserDeviceDataController 类中新增方法
    public static Map<Long, SseEmitter> getSseEmitters() {
        return sseEmitters;
    }

    /**
     * 根据用户选择的日期查询设备数据
     *
     * @param requestBody JSON 参数：userId, startDate, endDate
     * @return 用户设备数据列表  数组类型
     */
    @PostMapping("/userDataByDate")
    public List<UserDeviceData> getUserDataByDate(@RequestBody Map<String, Object> requestBody) {
        System.out.println("Received request body: " + requestBody);
        return userDeviceDataService.getUserDataByDate(requestBody);
    }

    /**
     * 获取当前登录用户的性别、年龄、身高
     */
    @GetMapping("/getAppUserInfo")
    public TableDataInfo getUserDeviceData(@RequestParam Long userId) {

        // 查询用户设备数据
        List<UserDeviceData> userInfoList = userDeviceDataService.getUserInfoByUserId(userId);

        return getDataTable(userInfoList);
    }

    /**
     * 获取最近七次用户健康数据
     *
     * @param userId 用户ID
     * @return 用户健康数据列表
     */
    @GetMapping("/sevensleeptime")
    public TableDataInfo getSevenSleepDuration(@RequestParam Long userId) {
        List<UserDeviceData> list = userDeviceDataService.getWeekUserLastestSleepTimeData(userId);
        return getDataTable(list);
    }


    /**
     * 获取用户最近五次血氧数据
     *
     * @param userId 用户ID
     * @return 血氧数据列表
     */
    @GetMapping("/userfivebloodoxygen")
    public TableDataInfo getUserFiveBloodOxygenData(@RequestParam Long userId) {
        List<UserDeviceData> list = userDeviceDataService.selectUserFiveBloodOxygenData(userId);
        return getDataTable(list);
    }
    @GetMapping("/getActiveTime")
    public Integer getActiveMinutes(@RequestParam Long userId) {
        Integer activeMinutes = userDeviceDataService.selectActiveMinutes(userId);
        return activeMinutes;
    }



}
